//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;

namespace DNAide.Configuration
{

	/// <summary></summary>
	public class HttpCompressionModuleRuleConfig : ConfigurationElement
	{
		internal object _lock = new object();
		private Regex _urlMatchExpressionRegex;


		/// <summary>
		/// The Url Match Expression as a Regex object
		/// </summary>
		public Regex UrlMatchExpressionRegex
		{
			get
			{
				if (_urlMatchExpressionRegex == null)
				{
					lock (_lock)
					{
						if (_urlMatchExpressionRegex == null)
						{
							if (DNAideConfigurationSettings.Web.HttpCompressionModule.RegexCompilationMode == RegexCompilationType.Compiled)
							{
								_urlMatchExpressionRegex = new Regex(DNAide.Web.UrlAide.Resolve(UrlMatchExpression), RegexOptions.IgnoreCase | RegexOptions.Compiled);
							}
							else
							{
								_urlMatchExpressionRegex = new Regex(DNAide.Web.UrlAide.Resolve(UrlMatchExpression), RegexOptions.IgnoreCase);
							}
						}
					}
				}
				return _urlMatchExpressionRegex;
			}
		}


		[ConfigurationProperty("enabled", DefaultValue = true, IsRequired = false)]
		public Boolean Enabled
		{
			get
			{
				return (bool)this["enabled"];
			}
			set
			{
				this["enabled"] = value;
			}
		}


		/// <summary>
		/// Regular expression to match against the requested url.
		/// </summary>
		[ConfigurationProperty("urlMatchExpression", IsRequired = true)]
		public String UrlMatchExpression
		{
			get
			{
				return (string)this["urlMatchExpression"];
			}
			set
			{
				this["urlMatchExpression"] = value;
			}
		}

	}


	/// <summary>HttpCompressionModuleRuleConfigCollection</summary>
	public class HttpCompressionModuleRuleConfigCollection : ConfigurationElementCollection, ICollection<HttpCompressionModuleRuleConfig>
	{
		/// <summary>IsReadOnly</summary>
		public new bool IsReadOnly
		{
			get
			{
				return base.IsReadOnly();
			}
		}

		/// <summary>HttpCompressionModuleRuleConfig</summary>
		public HttpCompressionModuleRuleConfig this[int index]
		{
			get
			{
				return (HttpCompressionModuleRuleConfig)BaseGet(index);
			}
			set
			{
				if (BaseGet(index) != null)
				{
					BaseRemoveAt(index);
				}
				BaseAdd(index, value);
			}
		}

		/// <summary>HttpCompressionModuleRuleConfig</summary>
		new public HttpCompressionModuleRuleConfig this[string mimeType]
		{
			get
			{
				return (HttpCompressionModuleRuleConfig)BaseGet(mimeType);
			}
		}

		/// <summary>HttpCompressionModuleRuleConfigCollection</summary>
		public HttpCompressionModuleRuleConfigCollection()
		{
		}

		/// <summary>Add</summary>
		public void Add(HttpCompressionModuleRuleConfig item)
		{
			if (item == null)
				return;

			BaseAdd(item);
		}

		/// <summary>Clear</summary>
		public void Clear()
		{
			BaseClear();
		}

		/// <summary>Contains</summary>
		public bool Contains(HttpCompressionModuleRuleConfig item)
		{
			if (BaseIndexOf(item) >= 0)
				return true;
			else
				return false;
		}

		/// <summary>CopyTo</summary>
		public void CopyTo(HttpCompressionModuleRuleConfig[] array, int arrayIndex)
		{
			((ICollection)this).CopyTo(array, arrayIndex);
		}

		/// <summary>CreateNewElement</summary>
		protected override ConfigurationElement CreateNewElement()
		{
			return new HttpCompressionModuleRuleConfig();
		}

		/// <summary>IndexOf</summary>
		public int IndexOf(HttpCompressionModuleRuleConfig item)
		{
			if (item == null)
				return -1;

			return BaseIndexOf(item);
		}

		/// <summary>GetElementKey</summary>
		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((HttpCompressionModuleRuleConfig)element).UrlMatchExpression;
		}

		/// <summary>GetEnumerator</summary>
		public new IEnumerator<HttpCompressionModuleRuleConfig> GetEnumerator()
		{
			for (int i = 0; i < Count; i++)
			{
				yield return (HttpCompressionModuleRuleConfig)this[i];
			}
		}

		/// <summary>Remove</summary>
		public bool Remove(HttpCompressionModuleRuleConfig item)
		{
			if (item == null)
				return false;

			if (BaseIndexOf(item) >= 0)
				BaseRemove(item.UrlMatchExpression);

			return true;
		}

		/// <summary>Remove</summary>
		public void Remove(string mimeType)
		{
			BaseRemove(mimeType);
		}

		/// <summary>RemoveAt</summary>
		public void RemoveAt(int index)
		{
			BaseRemoveAt(index);
		}

	}

}
